package com.lottery.game.controller.v2.bet;

import com.lottery.game.common.Response;
import com.lottery.game.constants.RespErrorCodes;
import com.lottery.game.core.modules.entity.game.GameRoom;
import com.lottery.game.core.modules.entity.user.GameIssue;
import com.lottery.game.core.modules.service.app.BetService;
import com.lottery.game.core.modules.service.app.RoomService;
import com.lottery.game.core.utils.BetUtils;

import io.swagger.annotations.*;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import lombok.extern.slf4j.Slf4j;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 竞猜
 */
@Slf4j
@RestController
@RequestMapping(value="/api/v2/bet")
@Api(tags="APP-下注管理接口")
public class BetApiController {

    @Autowired
    private BetService betService;

    @Autowired
    private RoomService roomService;

//    @Autowired
//    private CloudGameRunner cloudgameRunner;

    /**
     * 获取游戏期次
     *
     * @param roomNumber
     * @return
     */
    @SuppressWarnings("unchecked")
    @ApiOperation(value = "房间最新期次获取", notes = "房间最新期次获取", httpMethod = "GET")
    @ApiResponses(value = {@ApiResponse(code = 0, message = "", response = Response.class)})
    @ApiImplicitParam(value = "房间号", name = "roomNumber", example = "19")
    @GetMapping(value = "/get/game/issue", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Response<GameIssue> getGameIssue(@RequestParam("roomNumber") String roomNumber) {
        GameRoom room = roomService.queryRoomByRoomNo(roomNumber);
        //判断是否到维护时间/api/v2/chat/img/upload
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        String nowStr = sdf.format(now);

        if (room == null) {
            return Response.buildError(RespErrorCodes.NO_ROOM_FOUND_ERROR.getErrorMsg());
        }

        // 维护
        if (room.getMaintenance_start_time() != null && room.getMaintenance_end_time() != null) {
            try {
                int isDown = BetUtils.isShutDown(nowStr, room.getMaintenance_start_time(), room.getMaintenance_end_time());
                if (isDown != -1) {
                    return Response.buildError(RespErrorCodes.SYSTEM_MAINTENANCE_ERROR.getErrorMsg());
                }
            } catch (Exception e) {
                log.error(RespErrorCodes.UNKNOWN_ERROR.errorCode(), e);
                return Response.buildError(RespErrorCodes.UNKNOWN_ERROR.getErrorMsg());
            }
        }

        try {
            List<GameIssue> list = betService.queryRunningGameIssue(roomNumber);
            if (CollectionUtils.isEmpty(list)) {
                list = new ArrayList<>();
            }
            return Response.build(list);
        } catch (Exception e) {
            log.error("获取游戏期次发生错误", e);
            return Response.buildError(RespErrorCodes.UNKNOWN_ERROR.getErrorMsg());
        }

    }

    /**
     * 获取游戏赔率
     *
     * @param roomNumber
     * @return
     */
    @SuppressWarnings("unchecked")
    @ApiOperation(value = "游戏赔率", notes = "获取游戏赔率", httpMethod = "GET")
    @ApiResponses(value = {@ApiResponse(code = 0, message = "", response = Response.class)})
    @ApiImplicitParams({@ApiImplicitParam(name = "roomNumber", value = "房间号", example = "19", required = true)})
    @GetMapping(value = "/get/game/odd", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Response<Map<String, Object>> getGameOdd(@RequestParam("roomNumber") String roomNumber) {
        Map<String, Object> oddMap = betService.getOddbyRoomNumber(roomNumber);
        try {
            if (oddMap == null) {
                oddMap = new HashMap<>();
            }
            return Response.build(oddMap);
        } catch (Exception e) {
            log.error("获取游戏赔率发生错误", e);
            return Response.buildError(RespErrorCodes.UNKNOWN_ERROR.getErrorMsg());
        }
    }

    /**
     * 房间游戏记录
     *
     * @param roomNumber
     * @param pageNo
     * @param pageSize
     * @return
     */
    @SuppressWarnings("unchecked")
    @ApiOperation(value = "房间历史开奖结果", notes = "房间历史开奖结果", httpMethod = "GET")
    @ApiImplicitParams({@ApiImplicitParam(name = "roomNumber", value = "房间编码", example = "19", required = true), @ApiImplicitParam(name = "pageNo", value = "页数", dataType = "Integer", required = true, example = "1"), @ApiImplicitParam(name = "pageSize", value = "每页记录数", dataType = "Integer", required = true, example = "10"),})
    @ApiResponses(value = {@ApiResponse(code = 0, message = "", response = Response.class)})
    @GetMapping(value = "/get/room/game/record", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Response<List<GameIssue>> getRoomGameRecord(@RequestParam("roomNumber") String roomNumber, @RequestParam("pageNo") Integer pageNo, @RequestParam("pageSize") Integer pageSize) {
        if (pageNo == null || pageSize == null || pageNo == 0 || pageSize == 0) {
            return Response.buildError(RespErrorCodes.ROOM_LIST_PAGE_PARAM_ERROR.getErrorMsg());
        }
        try {
            int start = (pageNo - 1) * pageSize;
            List<GameIssue> list = betService.getRoomGameRecord(roomNumber, start, pageSize);
            return Response.build(list);
        } catch (Exception e) {
            log.error("获取房间游戏记录发生错误", e);
            return Response.buildError(RespErrorCodes.UNKNOWN_ERROR.getErrorMsg());
        }
    }


    /**
     * 回水记录
     *
     * @return
     */
    @SuppressWarnings("unchecked")
    @ApiIgnore
    @ApiOperation(value = "回水记录", notes = "获取回水记录", httpMethod = "GET")
    @ApiResponses(value = {@ApiResponse(code = 0, message = "", response = Response.class)})
    @GetMapping(value = "/getHuishuiGuiZe", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Response<String> getHuishuiGuiZe() {
        try {
            String str = betService.getHuishuiGuiZe();
            return Response.build(str);
        } catch (Exception e) {
            log.error("获取回水记录发生错误", e);
            return Response.buildError(RespErrorCodes.UNKNOWN_ERROR.getErrorMsg());
        }
    }
}